define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'tongcheng/module/index' + location.search,
                    add_url: 'tongcheng/module/add',
                    edit_url: 'tongcheng/module/edit',
                    del_url: 'tongcheng/module/del',
                    multi_url: 'tongcheng/module/multi',
                    import_url: 'tongcheng/module/import',
                    table: 'tongcheng_module',
                }
            });

            var table = $("#table");

            // 在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
              let form = $("form", table.$commonsearch);

              $("input[name='zone_id']", form).addClass("selectpage").data("source", "tongcheng/zone/index").data("params", {"custom[status]": 1}).data("orderBy","id desc");

              Form.events.cxselect(form);
              Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID')},
                        {field: 'zone_id', title: __('Zone_id'), visible: false},
                        // {
                        //     field: 'zone.name', title: __('Zone.name'), operate: false, formatter: function (value, row, index) {
                        //         return value + '(' + row.zone_id + ')';
                        //     }
                        // },
                        {field: 'type', title: __('Type'), searchList: {"BUY":__('BUY'),"SEND":__('SEND'),"TAKE":__('TAKE'),"WAN":__('WAN'),"1V1":__('1V1')}, formatter: Table.api.formatter.normal},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        // {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"0":__('Status 0')}, formatter: Table.api.formatter.status},
                        {field: 'is_show', title: __('is_show'), searchList: {"1":__('is_show 1'),"0":__('is_show 0')}, formatter: Table.api.formatter.status},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                  name: 'config',
                                  text: '基础配置',
                                  title: '基础配置',
                                  icon: 'fa fa-cogs',
                                  classname: 'btn btn-xs btn-success btn-dialog btn-config',
                                  extend: 'data-area=\'["60%", "80%"]\'',
                                  url: function (row) {
                                      console.log('row', row);
                                      return 'tongcheng/module_config/edit?module_id=' + row.id;
                                  },
                                },
                                {
                                  name: 'child',
                                  text: '类型列表',
                                  title: '类型列表',
                                  icon: 'fa fa-list',
                                  classname: 'btn btn-xs btn-info btn-dialog btn-child',
                                  extend: 'data-area=\'["80%", "80%"]\'',
                                  url: function (row) {
                                      console.log('row', row);
                                      return 'tongcheng/module_type/index?module_id=' + row.id + '&zone_id=' + row.zone_id;
                                  },
                                },
                                {
                                  name: 'range',
                                  text: '里程计费',
                                  title: '里程计费',
                                  icon: 'fa fa-cog',
                                  classname: 'btn btn-xs btn-primary btn-dialog btn-range',
                                  extend: 'data-area=\'["80%", "80%"]\'',
                                  url: function (row) {
                                      console.log('row', row);
                                      return 'tongcheng/module_config/range?module_id=' + row.id;
                                  },
                                },
                                // {
                                //   name: 'weight',
                                //   text: '重量配置',
                                //   title: '重量配置',
                                //   icon: 'fa fa-cog',
                                //   classname: 'btn btn-xs btn-danger btn-dialog btn-weight',
                                //   extend: 'data-area=\'["80%", "80%"]\'',
                                //   url: function (row) {
                                //       console.log('row', row);
                                //       return 'tongcheng/module_config/weight?module_id=' + row.id;
                                //   },
                                // },
                                {
                                  name: 'surcharge',
                                  text: '特殊时段配置',
                                  title: '特殊时段配置',
                                  icon: 'fa fa-cog',
                                  classname: 'btn btn-xs btn-danger btn-dialog btn-weight',
                                  extend: 'data-area=\'["80%", "80%"]\'',
                                  url: function (row) {
                                      console.log('row', row);
                                      return 'tongcheng/module_config/surcharge?module_id=' + row.id;
                                  },
                                },
                                {
                                  name: 'over_time_config',
                                  text: '超时理赔配置',
                                  title: '超时理赔配置',
                                  icon: 'fa fa-clock-o',
                                  classname: 'btn btn-xs btn-warning btn-dialog',
                                  extend: 'data-area=\'["80%", "80%"]\'',
                                  url: function (row) {
                                    return 'tongcheng/module_config/overtime?module_id=' + row.id;
                                  },
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'tongcheng/module/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'tongcheng/module/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'tongcheng/module/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
